package com.ruoyi.waterPrice.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.base.datam.MiMainchanB;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmacctY;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmfeeY;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.waterPrice.service.IWiFarmacctYService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 农户水费台账登记Controller
 *
 * @author ps
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/WiFarmacctY")
public class WiFarmacctYController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WiFarmacctYController.class);

    @Autowired
    private IWiFarmacctYService wiFarmacctYService;

    /**
     * app 获取水管员下属渠系  废弃
     */
    @GetMapping("app/getChanCdsByMainCd")
    public AjaxResult getChanCdsByMainCd(String mainCd){
        List<MiMainchanB> miMainchanBs = wiFarmacctYService.selectChanCds(mainCd);
        return AjaxResult.success(miMainchanBs);
    }

    /**
     * app 获取渠道下农户列表
     */
    @GetMapping("app/getFarmCdsByChanCd")
    public AjaxResult getFarmCdsByChanCd(String chanCd){
        HashMap<String, Object> chanCodeInfo = BusinessUtils.getChanCodeInfo(chanCd);
        String chanCdQuery = chanCodeInfo.get("resCode")+"";
        List<HashMap<String,String>> list = wiFarmacctYService.getFarmCdsByChanCd(chanCdQuery);
        return AjaxResult.success(list);
    }

    /**
     * app 管辖渠道的收费情况。
     */
    @GetMapping("app/getFeeByChan")
    public AjaxResult getFeeInfoByChan(String mainCd){
        ArrayList<HashMap<String, Object>> res = wiFarmacctYService.getFeeInfoByChan(mainCd);
        return AjaxResult.success(res);
    }

    /**
     * 农户水费收缴
     */
    @PostMapping("/payFarmFee")
    public AjaxResult payFarmFee(@RequestBody @Valid WiFarmacctY wiFarmacctY) throws Exception {
        log.info("农户水费收缴 入参：" + wiFarmacctY);
        int i = wiFarmacctYService.payFarmFee(wiFarmacctY);
        if (i == 1) {
            AjaxResult.success();
        } else {
            AjaxResult.error("农户缴费失败，请联系系统管理员~");
        }
        return AjaxResult.success();
    }

    /**
     * 上传附件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadPZFile")
    public AjaxResult uploadPZFile(@RequestParam("pzfile") MultipartFile file) throws IOException {
        String url = "";
        if (!file.isEmpty()) {
            try {
                // 上传并返回访问地址
                url = wiFarmacctYService.uploadFile(file);
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
        }
        return AjaxResult.success("上传成功", url);
    }


    /**
     * 查询农户水费台账登记列表
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:list")
    @GetMapping("/list")
    public TableDataInfo list(WiFarmacctY wiFarmacctY) {
        startPage();
        List<WiFarmacctY> list = wiFarmacctYService.selectWiFarmacctYList(wiFarmacctY);
        return getDataTable(list);
    }

    /**
     * 导出农户水费台账登记列表
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:export")
    @Log(title = "农户水费台账登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WiFarmacctY wiFarmacctY) {
        List<WiFarmacctY> list = wiFarmacctYService.selectWiFarmacctYList(wiFarmacctY);
        ExcelUtil<WiFarmacctY> util = new ExcelUtil<WiFarmacctY>(WiFarmacctY.class);
        util.exportExcel(response, list, "农户水费台账登记数据");
    }

    /**
     * 获取农户水费台账登记详细信息
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:query")
    @GetMapping(value = "/{feeYr}/{farmCd}")
    public AjaxResult getInfo(@PathVariable("feeYr") String feeYr, @PathVariable("farmCd") String farmCd) {
        log.info("获取农户水费台账登记详细信息 入参：" + feeYr + "=" + farmCd);
        WiFarmacctY wiFarmacctY = wiFarmacctYService.selectWiFarmacctYByFeeYrAndFarmCd(feeYr, farmCd);
        if (wiFarmacctY == null) {
            return AjaxResult.error("请先进行水费核定~");
        } else {
            return AjaxResult.success(wiFarmacctY);
        }
    }

    /**
     * 新增农户水费台账登记
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:add")
    @Log(title = "农户水费台账登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiFarmacctY wiFarmacctY) {
        return toAjax(wiFarmacctYService.insertWiFarmacctY(wiFarmacctY));
    }

    /**
     * 修改农户水费台账登记
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:edit")
    @Log(title = "农户水费台账登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiFarmacctY wiFarmacctY) {
        return toAjax(wiFarmacctYService.updateWiFarmacctY(wiFarmacctY));
    }

    /**
     * 删除农户水费台账登记
     */
    @RequiresPermissions("waterPrice:WiFarmacctY:remove")
    @Log(title = "农户水费台账登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{feeYrs}")
    public AjaxResult remove(@PathVariable String[] feeYrs) {
        return toAjax(wiFarmacctYService.deleteWiFarmacctYByFeeYrs(feeYrs));
    }
}
